package com.monkeyk.sos.web.controller.sys.resourcemgt;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.monkeyk.sos.domain.model.ResourceDetail;
import com.monkeyk.sos.service.ResInterfaceService;
import com.monkeyk.sos.service.ResourceService;
import com.monkeyk.sos.util.PageData;
import com.monkeyk.sos.util.Pager;
import com.monkeyk.sos.web.controller.BaseController;

//合作者管理
@Controller
@RequestMapping("/sys/resourcemgt")
public class ResourceMgtController extends BaseController {
	private static final Logger	Log	= Logger.getLogger(ResourceMgtController.class);
	
	@Autowired
	private ResourceService  resourceService;
	
	@Autowired
	private ResInterfaceService  resInterfaceService;
	
	
	/**
	 * 得到资源详情
	 * @param request
	 * @param response
	 * @param rows
	 * @param page
	 */
	
	@RequestMapping(value="/resourcedetail",method= {RequestMethod.POST , RequestMethod.GET})
	@ResponseBody
	public void getResourceDetail(HttpServletRequest request ,HttpServletResponse response,
		                      @RequestParam int rows,@RequestParam int page) {
	    
	    Log.info("资源详情: " + "/sys/resourcemgt/resourcedetail");
	    
	    List<ResourceDetail> resourceDetailList  = new ArrayList<ResourceDetail>();
	    //去后台查询所需参数 pageInfo cooperDetails
	    Pager pageInfo = new Pager(page, rows);
	    ResourceDetail resourceDetail = new ResourceDetail();
	    resourceDetailList = resourceService.getSelectedResourceDetail(resourceDetail, pageInfo);
	    int totalResults = resourceService.getAllCooperDetailsTotals();
	    //返回页面数据
	    PageData<ResourceDetail> pageData = new PageData<ResourceDetail>();
	    pageData.setRows(resourceDetailList);
	    pageData.setTotal(totalResults);;
	    writeJSON(response, pageData);
	    
	}
	
	/**
	 * 删除资源及对应接口
	 * @param request
	 * @param response
	 * @param cooperids
	 * @return
	 */
	@RequestMapping(value="/delresourcedetail",method=RequestMethod.POST)
	@ResponseBody
	public int   delResourceDetail(HttpServletRequest request,HttpServletResponse response,
		            @RequestParam("id") String resourceid, @RequestParam("ids") String resourceids) {
	    String[] id= resourceid.split(",");
	    String[] ids= resourceids.split(",");
	    for (int i = 0; i < ids.length; i++) {
		//删除选择资源
		resourceService.deleteResouceDetailByID(Long.parseLong(id[i]));
		resInterfaceService.deleteResInterfaceByRSID(ids[i]);
	    }
	    return 1;
	}
	
	
	
	/*下面是登录跳转页面*/
	
	/*
	 * 登录管理
	 */
	@RequestMapping("/resource_mgt")
	public String cooperatorMgt() {
	    Log.info("资源管理 : " + "/sys/resourcemgt/resource_mgt");
	    return "sys/resourcemgt/resource_mgt";
	}

}
